package com.site.action.backend.dining;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.site.dto.dining.DishesBizBean;
import com.site.service.dining.DishesService;
import com.site.util.PropertyUtils;
import com.site.util.base.action.BaseAction;
import com.site.util.base.service.BaseService;
import com.site.util.common.fileupload.FileUploadHandle;
import com.site.util.constant.Constants;

/**
 * 菜品Action
 * @author win7
 *
 */
public class DishesAction extends BaseAction {
	private static final long serialVersionUID = 6341544316035816442L;

	private List<DishesBizBean> dishesBizBeans;
	
	private DishesService dishesService;
	
	private DishesBizBean dishesBizBean;
	
	public DishesBizBean getDishesBizBean() {
		return dishesBizBean;
	}

	public void setDishesBizBean(DishesBizBean dishesBizBean) {
		this.dishesBizBean = dishesBizBean;
	}

	@Resource
	public void setDishesService(DishesService dishesService) {
		this.dishesService = dishesService;
	}

	public List<DishesBizBean> getDishesBizBeans() {
		return dishesBizBeans;
	}

	public void setDishesBizBeans(List<DishesBizBean> dishesBizBeans) {
		this.dishesBizBeans = dishesBizBeans;
	}
	
	/**
	 * 菜品
	 * @return
	 */
	public String getDishes(){
		if(queryPage == null){
			super.initPagination();
		}
		dishesBizBeans = dishesService.getDishes(queryPage,dishesBizBean);
		return "getDishes";
	}
	
	/**
	 * 菜品增加页面
	 * @return
	 */
	public String getDishesAdd(){
		return "getDishesAdd";
	}
	
	/**
	 *  菜品编辑页面
	 * @return
	 */
	public String getDishesEdit(){
		if(dishesBizBeans != null && !dishesBizBeans.isEmpty()){
			for (DishesBizBean dishesBizBean : dishesBizBeans) {
				if(null != dishesBizBean && dishesBizBean.getId() != null){
					dishesBizBean = dishesService.getEntityById(dishesBizBean.getId());
					PropertyUtils.copyNotNullProperties(this.dishesBizBean, dishesBizBean);
				}
			}
		}
		return "getDishesEdit";
	}
	
	/**
	 * 菜品编辑
	 * @return
	 */
	public String dishesEdit(){
		if(dishesBizBean.getId() != null){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dishesBizBean", dishesBizBean);
			if(null != files && !files.isEmpty()){
				// 上传对象 start 
				FileUploadHandle fileUploadHandle = new FileUploadHandle();
				fileUploadHandle.setRequest(request);
				// 取得上传文件类型
				fileUploadHandle.setAllowedTypes(Constants.UPDATE_FILE_TYPES);
				// 取得上传文件大小
				fileUploadHandle.setMaxSize(Constants.FILE_SIZE);
				fileUploadHandle.setFiles(files);
				fileUploadHandle.setFilesName(getFilesFileName());
				// 上传对象 end
				map.put("fileUploadHandle", fileUploadHandle);
			}
			dishesService.execute(map, BaseService.COMMAND_UPDATE);
			super.setMessageID("SYS_MSG_00022");
		}
		String url="";
		try {
			url = URLEncoder.encode("dishesBizBean.rmid="+dishesBizBean.getRmid()+"&amp;dishesBizBean.rmName="+dishesBizBean.getRmName()+"&dishesBizBean.rid="+dishesBizBean.getRid(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		super.setBackurl(request.getContextPath() + "/dishes/dishesAction!getDishes.do?"+url);
		return "toMessage";
	}
	
	/**
	 * 菜品删除
	 * @return
	 */
	public String dishesDelete(){
		if(dishesBizBeans != null && !dishesBizBeans.isEmpty()){
			Map<String, Object>  map = new HashMap<String, Object>();
			map.put("request", request);
			for (DishesBizBean dishesBizBean : dishesBizBeans) {
				if(null != dishesBizBean && dishesBizBean.getId() != null){
					map.put("dishesBizBean", dishesBizBean);
					dishesService.execute(map, BaseService.COMMAND_DELETE);
				}
			}
			super.setMessageID("SYS_MSG_00022");
		}
		String url="";
		try {
			url = URLEncoder.encode("dishesBizBean.rmid="+dishesBizBean.getRmid()+"&amp;dishesBizBean.rmName="+dishesBizBean.getRmName()+"&dishesBizBean.rid="+dishesBizBean.getRid(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		super.setBackurl(request.getContextPath() + "/dishes/dishesAction!getDishes.do?"+url);
		return "toMessage";
	}
	
	/**
	 * 增加
	 * @return
	 */
	public String dishesAdd(){
		if(dishesBizBean != null && checkData(dishesBizBean)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dishesBizBean", dishesBizBean);
			if(null != files && !files.isEmpty()){
				// 上传对象 start 
				FileUploadHandle fileUploadHandle = new FileUploadHandle();
				fileUploadHandle.setRequest(request);
				// 取得上传文件类型
				fileUploadHandle.setAllowedTypes(Constants.UPDATE_FILE_TYPES);
				// 取得上传文件大小
				fileUploadHandle.setMaxSize(Constants.FILE_SIZE);
				fileUploadHandle.setFiles(files);
				fileUploadHandle.setFilesName(getFilesFileName());
				// 上传对象 end
				map.put("fileUploadHandle", fileUploadHandle);
			}
			dishesService.execute(map, BaseService.COMMAND_ADD);
			super.setMessageID("SYS_MSG_00022");
		} else{
			return getDishesAdd();
		}
		String url="";
		try {
			url = URLEncoder.encode("dishesBizBean.rmid="+dishesBizBean.getRmid()+"&amp;dishesBizBean.rmName="+dishesBizBean.getRmName()+"&dishesBizBean.rid="+dishesBizBean.getRid(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		super.setBackurl(request.getContextPath() + "/dishes/dishesAction!getDishes.do?"+url);
		return "toMessage";
	}

	/**
	 * 数据检查
	 * @param dishesBizBean
	 * @return
	 */
	private boolean checkData(DishesBizBean dishesBizBean) {
		boolean mark = true;
		if(StringUtils.isEmpty(dishesBizBean.getTitle())){
			mark = false;
			super.addFieldError("title", "菜品名称不能为空！");
		}
		if(dishesBizBean.getPrice() == null){
			mark = false;
			super.addFieldError("price", "价格不能为空！");
		}
		return mark;
	}
	
}
